import { IJewelExchangeOper } from "./IJewelExchangeOper";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { ObjCreate } from "../../common/ObjCreate";
import { ImagesConst } from "../../../resource/ImagesConst";
import { Language } from "../../../../../extend/language/Language";
import { ResouceConst } from "../../../../component/gconst/ResouceConst";
import { TilePanel } from "../../equipment/TilePanel";
import { SpecialJewelOperRenderer } from "./SpecialJewelOperRenderer";
import { JewelExchangeOperateItem } from "./JewelExchangeOperateItem";
import { DataEvent } from "../../../events/DataEvent";
import { ItemData } from "../../../resource/info/ItemData";
import { EquipmentModuleConst } from "../../equipment/data/EquipmentModuleConst";
import { Cache } from "../../../cache/Cache";
import { SmeltConfig } from "../../../resource/SmeltConfig";
type int = number;
//class JewelExchangeSpecial
    
    export  class JewelExchangeSpecial /* flash.display.Sprite */ implements IJewelExchangeOper
    {
        constructor()
        {
            
            this._jewelDatas = new DataProvider();
            this.createChildren();
            this.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddedToStage);
            this.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onRemovedFromStage);
            return;
        }

        /* internal  */onAddedToStage(arg1: Object /* flash.events.Event */): void
        {
            Dispatcher.addEventListener(EventName.SmeltJewelTransSuccess, this.onSlemtJewelTransSuccess);
            return;
        }

        /* internal  */onRemovedFromStage(arg1: Object /* flash.events.Event */): void
        {
            Dispatcher.removeEventListener(EventName.SmeltJewelTransSuccess, this.onSlemtJewelTransSuccess);
            return;
        }

        /* internal  */createChildren(): void
        {
            var loc1=0;
            var loc2=0;
            var loc3=0;
            var loc4=null;
            ObjCreate.createBitmap(ImagesConst.Fire, 197, -32, this);
            ObjCreate.createLabel(Language.getString(69232), 220, -25, 186, 20, flash.text.TextFormatAlign.LEFT, this, true, 16776960);
            loc3 = 0;
            while (loc3 < 8) 
            {
                loc1 = 30 + loc3 % 2 * 190;
                loc2 = 5 + Math.floor(loc3 / 2) * 90;
                ObjCreate.createBg(loc1, loc2, 160, 20, this, ImagesConst.FightingStrgBg);
                (loc4 = ResouceConst.getScaleBitmap("PackItemBg")).x = 10 + loc3 % 2 * 193;
                loc4.y = 26 + Math.floor(loc3 / 2) * 90;
                loc4.setSize(66, 66);
                this.addChild(loc4);
                if (loc3 >= 1 && loc3 <= 3) 
                {
                    ObjCreate.createBg(10, 2 + loc3 * 90, 370, 2, this, ImagesConst.SplitLine);
                }
                ++loc3;
            }
            this._jewelOperTilePanel = new TilePanel();
            this._jewelOperTilePanel.setSize(390, 370);
            this._jewelOperTilePanel.x = 10;
            this._jewelOperTilePanel.y = 5;
            this._jewelOperTilePanel.columnWidth = 190;
            this._jewelOperTilePanel.rowHeight = 90;
            this._jewelOperTilePanel.setStyle("cellRenderer", SpecialJewelOperRenderer);
            this._jewelOperTilePanel.dataProvider = this._jewelDatas;
            this._jewelOperTilePanel.addEventListener(JewelExchangeOperateItem.MoveOutJewel, this.onMoveOutJewel);
            this.addChild(this._jewelOperTilePanel);
            return;
        }

        /* internal  */onMoveOutJewel(arg1: DataEvent): void
        {
            this.removeJewel(arg1.data as ItemData);
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_JewelExchangeUpdateJewels, null, false, true, true));
            return;
        }

        public addJewel(arg1: ItemData): void
        {
            var loc1=0;
            var loc2=0;
            Cache.instance.smelt.changeSelectedJewelNum(arg1, arg1.itemAmount);
            if (this._jewelDatas.length < this._size) 
            {
                loc2 = SmeltConfig.instance.getJewelTransCostByCode(arg1.type, arg1.itemInfo.item.itemLevel) * arg1.itemAmount;
                this._jewelDatas.addItem({"itemData": arg1, "transNum": arg1.itemAmount, "cost": loc2});
            }
            this._jewelOperTilePanel.dataProvider = this._jewelDatas;
            return;
        }

        public removeJewel(arg1: ItemData): void
        {
            var loc1=null;
            var loc2=0;
            Cache.instance.smelt.deleteSelectedJewel(arg1.uid);
            loc2 = 0;
            while (loc2 < this._jewelDatas.length) 
            {
                loc1 = this._jewelDatas.getItemAt(loc2)["itemData"] as ItemData;
                if (loc1.uid == arg1.uid) 
                {
                    this._jewelDatas.removeItemAt(loc2);
                    break;
                }
                ++loc2;
            }
            this._jewelOperTilePanel.dataProvider = this._jewelDatas;
            return;
        }

        public getTransJewelDic(): Map<any, any> /* flash.utils.Dictionary */
        {
            var loc2=null;
            var loc3=0;
            var loc4=null;
            var loc1=new Map<any, any> /* flash.utils.Dictionary */();
            loc3 = 0;
            while (loc3 < this._jewelDatas.length) 
            {
                loc2 = (loc4 = this._jewelDatas.getItemAt(loc3))["itemData"] as ItemData;
                loc1[loc2.uid] = loc4["transNum"];
                ++loc3;
            }
            return loc1;
        }

        public getTargetJewels(): Map<any, any> /* flash.utils.Dictionary */
        {
            return null;
        }

        public getCost(): int
        {
            var loc1=0;
            var loc2=0;
            var loc3=null;
            loc2 = 0;
            while (loc2 < this._jewelDatas.length) 
            {
                loc3 = this._jewelDatas.getItemAt(loc2);
                loc1 = loc1 + loc3["cost"];
                ++loc2;
            }
            return loc1;
        }

        public checkJewelLost(): void
        {
            var loc2=null;
            var loc3=0;
            var loc1=false;
            loc3 = 0;
            while (loc3 < this._jewelDatas.length) 
            {
                loc2 = this._jewelDatas.getItemAt(loc3)["itemData"] as ItemData;
                if (loc2 && this.isLost(loc2)) 
                {
                    loc1 = true;
                    Cache.instance.smelt.deleteSelectedJewel(loc2.uid);
                    this._jewelDatas.removeItemAt(loc3);
                }
                ++loc3;
            }
            if (loc1) 
            {
                this._jewelOperTilePanel.dataProvider = this._jewelDatas;
            }
            return;
        }

        /* internal  */isLost(arg1: ItemData): Boolean
        {
            var loc1=Cache.instance.pack.stonePackCache.getItemDataByUid(arg1.uid);
            if (loc1 && loc1.itemAmount == arg1.itemAmount) 
            {
                return false;
            }
            return true;
        }

        public hasFullPutIn(): Boolean
        {
            if (this._jewelDatas.length >= this._size) 
            {
                return true;
            }
            return false;
        }

        public hasJewelPutIn(): Boolean
        {
            if (this._jewelDatas.length > 0) 
            {
                return true;
            }
            return false;
        }

        public clearJewels(): void
        {
            var loc1=null;
            var loc2=0;
            loc2 = 0;
            while (loc2 < this._jewelDatas.length) 
            {
                loc1 = this._jewelDatas.getItemAt(loc2);
                Cache.instance.smelt.deleteSelectedJewel(loc1.itemData.uid);
                ++loc2;
            }
            this._jewelDatas = new DataProvider();
            this._jewelOperTilePanel.dataProvider = this._jewelDatas;
            return;
        }

        /* internal  */onSlemtJewelTransSuccess(arg1: DataEvent): void
        {
            var loc3=null;
            var loc4=0;
            var loc5=0;
            var loc6=0;
            this.clearJewels();
            var loc1=new DataProvider();
            var loc2=arg1.data as Array<any>;
            loc4 = 0;
            while (loc4 < loc2.length) 
            {
                loc3 = new ItemData(loc2[loc4]);
                Cache.instance.smelt.changeSelectedJewelNum(loc3, loc3.itemAmount);
                loc6 = SmeltConfig.instance.getJewelTransCostByCode(loc3.type, loc3.itemInfo.item.itemLevel) * loc3.itemAmount;
                loc1.addItem({"itemData": loc3, "transNum": loc3.itemAmount, "cost": loc6});
                ++loc4;
            }
            this._jewelDatas = loc1;
            this._jewelOperTilePanel.dataProvider = this._jewelDatas;
            return;
        }

        /* internal */ /* var */_size: int=8;

        /* internal */ /* var */_jewelOperTilePanel: TilePanel;

        /* internal */ /* var */_jewelDatas: DataProvider;
    }
